package com.kbbywyclient.presenter;

import android.content.Context;
import android.text.TextUtils;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.HotRec;
import com.kbbywyclient.data.entity.TaskTab;
import com.kbbywyclient.data.model.HotReModel;
import com.kbbywyclient.data.model.TaskTabModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.TaskContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.pre.PreUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;
/**
 * Created by Jiangzx on 2017/8/17 20:02.
 */

public class TaskPresenter extends BasePresenter implements TaskContract.Presenter {
    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil jsonUtil;

    public static final int START_PAGE=0;
    public int pageNo =START_PAGE;
    private List<HotRec> hotRecs;
    private TaskContract.View view;
    @Inject
    public TaskPresenter(TaskContract.View view){
        this.view=view;
        this.hotRecs=new ArrayList<>();
    }

    @Override
    public void loadTaskTab() {
        jsonUtil.put("parent_code", Constants.JBQ011102);
        if (PreUtils.isLogin(mContext)) {
            jsonUtil.put("saleID", String.valueOf(PreUtils.getUser(mContext).getSalesID()));
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getTaskTab(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<TaskTabModel>() {
                    @Override
                    public void onCompleted() {
                        view.finishRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.finishRefresh();
                    }

                    @Override
                    public void onNext(TaskTabModel taskTabModel) {
                        TaskTab taskTab = taskTabModel.getData();
                        if (taskTab != null) {
                            List<TaskTab.Bean> data = taskTab.getTabs();
                            if (data != null && data.size() > 0) {
                                view.onTaskTabSuccess(data);
                            }
                        }
                    }
                });
    }

    @Override
    public void loadTaskHotRec(int page, String saleId) {
        jsonUtil.put("Code",Constants.DCP021702);
        jsonUtil.put("pageNum",page);
        if(!TextUtils.isEmpty(saleId)){
            jsonUtil.put("saleID",saleId);
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.getHotRec(jsonUtil.getRsaParams())).
                subscribe(new Subscriber<HotReModel>() {
                    @Override
                    public void onCompleted() {
                        view.finishRefresh();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.finishRefresh();
                        pageNo--;
                        if(pageNo<START_PAGE){
                            pageNo=START_PAGE;
                        }
                        view.loadListEmpty();
                    }

                    @Override
                    public void onNext(HotReModel model) {
                        List<HotRec> data=model.getData();
                        if(data==null){
                            view.loadListEmpty();
                        }else{
                            if(page==START_PAGE){
                                hotRecs.clear();
                            }
                            if(data.size()>0){
                                hotRecs.addAll(data);
                            }
                            if(page==model.getTotal_page()-1){
                                //没有更多数据了
                                view.setListUi(hotRecs,false);
                            }else{
                                view.setListUi(hotRecs,true);
                            }
                        }
                    }
                });
    }

    @Override
    public void clearList() {
        pageNo=START_PAGE;
        if(hotRecs!=null){
            hotRecs.clear();
        }
    }
}
